home *** CD-ROM | disk | FTP | other *** search
- #include <QDOffscreen.h>
- #include "Anti-Aliased Text.h"
-
-
-
-
-
- void FourtoOneDitherTo4BitGrey(PixMapHandle bwMap,PixMapHandle colorMap);
-
-
-
-
-
- short AADrawString(StringPtr text)
- {
- GWorldPtr bwGWorld,colorGWorld;
- CTabHandle cTable;
- GrafPtr curPort;
- FontInfo fInfo;
- CGrafPtr portSave;
- GDHandle gdSave;
- Style textFace;
- Rect bwRect,colorRect,destRect;
- short textSize,textFont,width,height,smallAscent,err;
-
-
- GetPort(&curPort);
- GetFontInfo(&fInfo);
-
- smallAscent = fInfo.ascent;
- textFace = curPort->txFace;
- textFont = curPort->txFont;
- textSize = curPort->txSize;
- TextSize(textSize*4);
-
- width = StringWidth(text);
- width += 32-(width%32);
-
- GetFontInfo(&fInfo);
- height = fInfo.ascent+fInfo.descent;
- height += 4-(height%4);
-
- TextSize(textSize);
-
- SetRect(&bwRect,0,0,width,height);
- SetRect(&colorRect,0,0,width/4,height/4);
- destRect = colorRect;
- OffsetRect(&destRect,curPort->pnLoc.h,curPort->pnLoc.v-smallAscent);
-
- err = NewGWorld(&bwGWorld,1,&bwRect,nil,nil,0);
- if (err != noErr)
- return err;
-
- cTable = GetCTable(36);
- err = NewGWorld(&colorGWorld,4,&colorRect,cTable,nil,0);
- if (err != noErr)
- {
- DisposeGWorld(bwGWorld);
- DisposCTable(cTable);
- return err;
- }
-
- LockPixels(GetGWorldPixMap(bwGWorld));
- LockPixels(GetGWorldPixMap(colorGWorld));
-
- GetGWorld(&portSave,&gdSave);
- SetGWorld(bwGWorld,nil);
-
-
- EraseRect(&bwRect);
-
- TextSize(textSize*4);
- TextFont(textFont);
- TextFace(textFace);
-
- MoveTo(0,fInfo.ascent);
- DrawString(text);
-
- FourtoOneDitherTo4BitGrey(GetGWorldPixMap(bwGWorld),GetGWorldPixMap(colorGWorld));
-
-
- SetGWorld(portSave,gdSave);
-
- CopyBits((BitMap*)*GetGWorldPixMap(colorGWorld),&curPort->portBits,&colorRect,&destRect,curPort->txMode,nil);
-
- UnlockPixels(GetGWorldPixMap(bwGWorld));
- UnlockPixels(GetGWorldPixMap(colorGWorld));
-
- DisposeGWorld(bwGWorld);
- DisposeGWorld(colorGWorld);
- DisposCTable(cTable);
-
- return noErr;
- }
-
-
-
-
-
- void FourtoOneDitherTo4BitGrey(PixMapHandle bwMap,PixMapHandle colorMap)
- {
- Ptr bwPtr,colorPtr;
- short height,width,bwRows,colorRows;
- long colorRowDiff,bwChunkMod;
- Byte *bwDataPtr,*colorDataPtr;
- char mode;
-
-
- height = (*colorMap)->bounds.bottom-(*colorMap)->bounds.top;
- width = ((*bwMap)->bounds.right-(*bwMap)->bounds.left)/8;
- bwRows = (*bwMap)->rowBytes&0x3FFF;
- colorRows = (*colorMap)->rowBytes&0x3FFF;
- colorRowDiff = colorRows-width;
- bwChunkMod = 4*bwRows-width;
-
- bwPtr = GetPixBaseAddr(bwMap);
- colorPtr = GetPixBaseAddr(colorMap);
-
- mode = true32b;
- SwapMMUMode(&mode);
-
- asm
- {
- movem.l A0-A4/D0-D7,-(SP) ; save registers
-
- move.l colorPtr,A2 ; color data ptr
- move.l bwPtr,A0 ; bw data ptr
- moveq #0,D6 ; clear D6 for long value
- move.w bwRows,D6 ; width of bw pixmap row data
- lea @data,A3 ; store lookup table ptr
- moveq #0,D0 ; clear D0 for long value
- move.w bwRows,D0 ; offset from the right to the left of a chunk
- lsl.l #2,D0 ; offset from the top to the bottom of a chunk
- neg.l D0 ; offset from the bottom to the top of a chunk
- addq.w #1,D0 ; add offset to next chunk
-
- @row move.w width,D7 ; loop this many bytes across
-
- @adv moveq #0,D3 ; initial color result low nibble
- moveq #0,D4 ; initial color result high nibble
- moveq #4,D5 ; set up inner loop counter
-
- @sum move.b (A0),D1 ; get next byte (D1 gets high nibble, D2 gets low nibble)
- move.b D1,D2 ; copy for low nibble
- andi.w #0x000F,D2 ; get only low nibble
- andi.w #0x00F0,D1 ; get only high nibble
- lsr.w #4,D1 ; put high nibble in low nibble for lookup
-
- movea.w D2,A1 ; get table offset
- adda.l A3,A1 ; compute offset from start of table
- add.b (A1),D3 ; add #bits in low nibble to color result
- movea.w D1,A1 ; get table offset
- adda.l A3,A1 ; compute offset from start of table
- add.b (A1),D4 ; add #bits in high nibble to color result
-
- adda.l D6,A0 ; go to the next line
- subq.w #1,D5 ; subract one line of chunk
- bne.s @sum ; branch until we've summed both 4x4 blocks
-
- btst #4,D3 ; is high nibble off ramp scale?
- beq.s @1 ; nope, continue
- moveq #0x0F,D3 ; clip it to the top of the ramp
- @1 lsl.b #4,D4 ; shift low resultant nibble to high nibble
- bcc.s @2
- move.b #0xF0,D4 ; clip it to the top of the ramp
- @2 or.w D4,D3 ; or both nibbles into a byte
- move.b D3,(A2)+ ; write the byte into the color pixmap
-
- adda.l D0,A0 ; move bw data ptr to the next chunk
- subq.w #1,D7 ; subtract another column
- bne.s @adv ; branch until we've advanced across a row
-
- add.l bwChunkMod,A0 ; move bw data ptr to the beginning of the next chunk
- add.l colorRowDiff,A2 ; move color data ptr to the beginning the next line
- subi.w #1,height ; subtract another row
- bne.s @row ; branch until we've covered every row
-
- movem.l (SP)+,A0-A4/D0-D7 ; restore registers
- bra.s @dataEnd
-
- @data dc.l 0x00010102
- dc.l 0x01020203
- dc.l 0x01020203
- dc.l 0x02030304
- @dataEnd
- }
-
- SwapMMUMode(&mode);
- }